<?php

namespace app\home\controller;

use app\home\lib\MyController;
use app\home\lib\MyPhpExcel;
use think\Request;

class QaBank extends MyController
{
	// 题目列表
	public function list()
	{
		$qa_main_id = input('qa_main_id');

		$page = input('page');
		$search = Request::instance()->post('search/a');

		$qaMain = db("qa_main")->where("id", $qa_main_id)->find();

		if (empty($search)) {
			$search = array(
				'search_value' => input('search_value'),
			);
		}

		if (!isset($page) || $page < 1) {
			$page = 1;
		}

		$where['qa_main_id'] = $qa_main_id;
		if (empty($search['search_value'])) {
			$search['search_value'] = '';
		} else {
			$where['title'] = ['like', "%{$search['search_value']}%"];
		}

		$countInfo = db('qa_bank')->field("count(1) count")->where($where)->find();
		$count = $countInfo['count'];
		$allPage = ceil($count / 10);
		if ($page > $allPage) {
			$page = $allPage;
		}

		$qaBankList = db("qa_bank")->where($where)->page($page, $this->onePage)->order('q_no asc')->select();
		foreach ($qaBankList as $key => $qaBank) {
			$qaBankList[$key]['qa_json'] = json_decode($qaBank['qa_json'], true);
		}

		$qaIndexPageInfo = cache(session_id() . "qaIndexPageInfo");
		$this->assign('qaIndexPageInfo', $qaIndexPageInfo);
		$this->assign('page', $page);
		$this->assign('count', $count);
		$this->assign('allpage', $allPage);
		$this->assign('qa_main_id', $qa_main_id);
		$this->assign('search', $search);
		$this->assign('qaBankList', $qaBankList);
		return $this->fetch();
	}

	// 编辑页面
	// public function add()
	// {
	// 	$id = input('id');
	// 	$qaMainId = input('qaMainId');

	// 	if (!empty($id)) {
	// 		$qaBank = db('qa_bank')->where('id', $id)->find();

	// 		$qaBank['qa_json'] = json_decode($qaBank['qa_json'], true);
	// 		$this->assign('qaBank', $qaBank);
	// 	} else {
	// 		$qaBank = [];
	// 		$qaBank['type_tag'] = '单选';
	// 		$this->assign('qaBank', $qaBank);
	// 	}

	// 	$qaMainPageInfo = cache(session_id() . "qaMainPageInfo");

	// 	$this->assign('qaMainId', $qaMainId);
	// 	$this->assign('qaMainPageInfo', $qaMainPageInfo);

	// 	return $this->fetch();
	// }

	// 删除题目
	public function deleteForAjax()
	{
		$id = input('id') * 1;
		$qa_main_id = input('qa_main_id') * 1;
		if ($id < 1 || $qa_main_id < 1) {
			return json(array('code' => '-1', 'msg' => "id不能为空！", 'data' => $id));
		}
		$res = db('qa_bank')->where('id', $id)->delete();

		return json(array('code' => '0', 'msg' => "删除成功", 'data' => $res));
	}

	// 编辑题目
	public function editForAjax()
	{
		$data = input('post.');
		if (empty($data['title'])) {
			return json(array('code' => '-1', 'msg' => "题目不能为空！", 'data' => $data));
		}

		if (empty($data['type_tag'])) {
			return json(array('code' => '-2', 'msg' => "题目类型不能为空！", 'data' => $data));
		}

		$score = 0;
		foreach ($data['qa_json'] as $k => $v) {
			if (!empty($v['text'])) {
				// $v['score'] = $v['score'] > 0 ? $v['score'] : 0;
				$v['score'] = is_numeric($v['score']) ? $v['score'] : 0;
				if ($data['type_tag'] == '多选-数量记分') {
					$v['score'] = 0;
				} else if ($data['type_tag'] == '多选-合计记分') {
					// 多选题 分数和
					$score += $v['score'];
				} else if ($score < $v['score']) {
					// 单选题选最高分
					$score = $v['score'];
				}
				$data['qa_json'][$k] = array('score' => $v['score'], 'text' => $v['text']);
			} else {
				unset($data['qa_json'][$k]);
			}
		}

		if (empty($data['qa_json'])) {
			return json(array('code' => '-3', 'msg' => "答案不能为空！", 'data' => $data));
		}

		$data['count_mode'] = [];
		if ($data['type_tag'] == '多选-数量记分') {
			$mcAbove = $data['count_mode_above']; //大于
			$mcScore = $data['count_mode_score']; //得分

			if ($mcAbove > 0 && $mcScore > 0) {
				$data['count_mode'] = ['mode' => 'above_count', 'above' => $mcAbove, 'score' => $mcScore];
				$score = $mcScore;
				$data['type_tag'] = '多选';
			} else {
				return json(array('code' => '-3', 'msg' => "选择个数和得分都要大于0！", 'data' => $data));
			}
		} else if ($data['type_tag'] == '多选-合计记分') {
			$data['type_tag'] = '多选';
		}

		// 按分数排序
		// $key = array_column(array_values($data['qa_json']), 'score');
		// array_multisort($key, SORT_DESC, $data['qa_json']);
		$data['qa_json'] = json_encode($data['qa_json']);
		$data['count_mode'] = json_encode($data['count_mode']);
		$data['score'] = $score;

		$id = $data['id'];
		unset($data['id']);
		unset($data['count_mode_above']);
		unset($data['count_mode_score']);

		if (empty($id)) {
			$data['status'] = 1;
			$id = db('qa_bank')->insertGetId($data);
		} else {
			$res = db('qa_bank')->where('id', $id)->update($data);
		}
		$data['id'] = $id;
		return json(array('code' => '0', 'msg' => "更新成功", 'data' => $data));
	}

	public function changeStatus()
	{
		$id = input('id');
		$status = input('status');

		if (empty($id)) {
			$this->error('数据为空！');
		}

		// 切换状态
		$res = db('qa_bank')->where('id', $id)->update(['status' => $status == 0 ? 1 : 0]);

		$qaMainPageInfo = cache(session_id() . "qaMainPageInfo");
		$tiaozhuan = "id=" . $qaMainPageInfo['id'] .
			"&page=" . $qaMainPageInfo['page'] .
			"&tab=2" .
			"&search_value=" . $qaMainPageInfo['search_value'];
		if ($res) {
			$this->success('更新成功！', "Qa/add?" . $tiaozhuan);
		} else {
			$this->error('未更新任何数据！', "Qa/add?" . $tiaozhuan);
		}
	}

	// 导出数据
	// excel导出
	// http://192.168.8.130/azfzjkp_dev/home/QaBank/exportExcel.html
	public function exportExcel()
	{
		// $url = (new MyPhpExcel())->getExcel($qa_main_id);

		// $url = 'https://view.officeapps.live.com/op/view.aspx?src=' . urlencode($url . "?t=" . time());
		header("Location: {$url}");
		return;
	}

	// excel导入
	// http://192.168.8.130/azfzjkp_dev/home/QaBank/importExcel/qa_main_id/96.html?excel_url=https://uploads.jiankangpao.sichuancancer.org/uploads/other/20230417/c3129ad17732e8304f8781e365687c72.xls
	// http://192.168.8.130/azfzjkp_dev/home/QaBank/importExcel/qa_main_id/96.html?excel_url=https://uploads.jiankangpao.sichuancancer.org/uploads/other/20230417/4c0b546e2483e00e5044e46dd75adb65.xlsx
	// public function importExcel()
	// {
	// 	// append:追加导入；replace：全新导入
	// 	$qa_main_id = input('qa_main_id');
	// 	$excel_url = input('excel_url/s');
	// 	$import_type = input('import_type/s', 'append');

	// 	$rt = (new MyPhpExcel())->setExcel($qa_main_id, $import_type, $excel_url);
	// 	if ($rt['code'] > 0) {
	// 		$this->success($rt['msg'], "list?qa_main_id={$qa_main_id}");
	// 	} else {
	// 		$this->error($rt['msg'], "list?qa_main_id={$qa_main_id}");
	// 	}
	// }

	// ========================================================================================================
	
	
	// http://192.168.8.130/azfzjkp_dev/home/QaBank/readFile.html?excel_name=基本情况
	// http://192.168.8.130/azfzjkp_dev/home/QaBank/readFile.html?excel_name=闯关题
	// http://192.168.8.130/azfzjkp_dev/home/QaBank/readFile.html?excel_name=科普题
	// http://192.168.8.130/azfzjkp_dev/home/QaBank/readRegionFile.html

	// http://192.168.8.130/azfzjkp/home/QaBank/readFile.html?excel_name=基本情况
	// http://192.168.8.130/azfzjkp/home/QaBank/readFile.html?excel_name=闯关题
	// http://192.168.8.130/azfzjkp/home/QaBank/readFile.html?excel_name=科普题
	// http://192.168.8.130/azfzjkp/home/QaBank/readRegionFile.html
	public function readFile()
	{
		$excel_name = input('excel_name');

		$qaMain = db('qa_main')->where('title', $excel_name)->find();
		if (empty($qaMain)) {
			$this->error('参数不对');
		} else {
			$qa_main_id = $qaMain['id'];
		}

		$path = ROOT_PATH . "application/home/controller/excel/名医-肿瘤医院问答【{$excel_name}】.xls";
		var_dump($path);
		$rt = (new MyPhpExcel())->setExcel($qa_main_id, 'replace', $path);
		var_dump($rt);
		die();
	}

	public function readRegionFile()
	{
		$path = ROOT_PATH . "application/home/controller/excel/市州对应题目.xlsx";
		$rt = (new MyPhpExcel())->setRegionExcel($path);
		var_dump($rt);
	}
}
